from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from PIL import Image
import time
from Selenium import chaojiying

if __name__ == '__main__':
    #
    path = "chromedriver.exe"
    liu = webdriver.Chrome(path)
    url = "https://accounts.douban.com/passport/login?redir=https%3A%2F%2Fmovie.douban.com%2F"
    liu.get(url)
    time.sleep(2)
    button1 = liu.find_element_by_xpath("//ul[@class='tab-start']/li[2]")
    button1.click()
    time.sleep(2)
    input_name = liu.find_element_by_xpath("//input[@id='username']")
    input_name.send_keys("15325098743")
    time.sleep(2)
    input_pwd = liu.find_element_by_xpath("//input[@id='password']")
    input_pwd.send_keys("z141247830")
    time.sleep(2)
    btdl = liu.find_element_by_xpath("//div[@class='account-form-field-submit ']/a")
    btdl.click()
    time.sleep(3)
    liu.switch_to.frame("tcaptcha_iframe")
    time.sleep(2)
    img_src = liu.find_element_by_xpath("//div[@id='slideBgWrap']/img[@id='slideBg']")
    # left = img_src.location['x']
    # top = img_src.location['y']
    # right = left + img_src.size['width']
    # bottom = top + img_src.size['height']
    # print(left,top,right,bottom)
    # 打开登录页面的截图
    liu.save_screenshot("login.png")
    time.sleep(2)
    im = Image.open("login.png")
    # 获取登录按钮截图
    mg = im.crop((494, 324, 634, 520))
    # mg = im.crop((268,75,308,125))
    # 保存截图，命名未ann
    mg.save('ann.png')
    cjy = chaojiying.Chaojiying_Client('action', 'action', '96001')
    cjy_img = open('ann.png', 'rb').read()
    cjy_xy = cjy.PostPic(cjy_img, 9101)
    cjy_z = cjy_xy.get("pic_str")
    print(cjy_z)
    list_xy = cjy_z.split(",")
    time.sleep(2)
    # x=53
    # ActionChains(liu).move_to_element_with_offset(img_src, 53,73).context_click().perform()
    ActionChains(liu).move_to_element_with_offset(img_src, 53, list_xy[1]).click().perform()
    time.sleep(1)
    ActionChains(liu).move_by_offset(xoffset=int(list_xy[0]) + 200 - 60, yoffset=1).perform()
    time.sleep(1)
    ActionChains(liu).click().perform()
    print(list_xy[0], list_xy[1])
    time.sleep(99999)
